﻿namespace MsForgeReboot.Services {
    #region Usings

    using System.ComponentModel.DataAnnotations.Schema;
    using System.Data.Entity;
    using MsForgeReboot.Framework.Entities;

    #endregion

    /// <summary>
    /// </summary>
    public class MsForgeContext : DbContext {
        #region Public Properties

        /// <summary>
        ///   Gets or sets the blog posts.
        /// </summary>
        /// <value> The blog posts. </value>
        public virtual IDbSet<BlogPost> BlogPosts { get; set; }

        /// <summary>
        ///   Gets or sets the comments.
        /// </summary>
        /// <value> The comments. </value>
        public virtual IDbSet<Comment> Comments { get; set; }

        /// <summary>
        ///   Gets or sets the feeds.
        /// </summary>
        /// <value> The feeds. </value>
        public virtual IDbSet<Feed> Feeds { get; set; }

        /// <summary>
        ///   Gets or sets the pages.
        /// </summary>
        /// <value> The pages. </value>
        public virtual IDbSet<Page> Pages { get; set; }

        /// <summary>
        ///   Gets or sets the sections.
        /// </summary>
        /// <value> The sections. </value>
        public virtual IDbSet<Section> Sections { get; set; }

        /// <summary>
        ///   Gets or sets the tags.
        /// </summary>
        /// <value> The tags. </value>
        public virtual IDbSet<Tag> Tags { get; set; }

        /// <summary>
        /// Gets or sets the users.
        /// </summary>
        /// <value>
        /// The users.
        /// </value>
        public virtual IDbSet<User> Users { get; set; }

        /// <summary>
        /// Gets or sets the roles.
        /// </summary>
        /// <value>
        /// The roles.
        /// </value>
        public virtual IDbSet<Role> Roles { get; set; }

        /// <summary>
        /// Gets or sets the Memeberships from SMP for reusing old logins
        /// </summary>
        public virtual IDbSet<Membership> Memberships { get; set; }

        /// <summary>
        /// Gets or sets the OAuthMembership data from the SMP
        /// </summary>
        public virtual IDbSet<OAuthMembership> OAuthMemberships { get; set; }

        /// <summary>
        /// Gets or sets the speakers.
        /// </summary>
        /// <value>
        /// The speakers.
        /// </value>
        public virtual IDbSet<Speaker> Speakers { get; set; }

        /// <summary>
        /// Gets or sets the events.
        /// </summary>
        /// <value>
        /// The events.
        /// </value>
        public virtual IDbSet<Event> Events { get; set; }

        /// <summary>
        /// Gets or sets the user groups.
        /// </summary>
        /// <value>
        /// The user groups.
        /// </value>
        public virtual IDbSet<UserGroup> UserGroups { get; set; }


        /// <summary>
        /// Gets or sets sent Cries.
        /// </summary>
        /// <value>
        /// The cries.
        /// </value>
        public virtual IDbSet<Cry> Cries { get; set; }

        /// <summary>
        /// Gets or sets statistics for a given cry.
        /// </summary>
        /// <value>
        /// The cry statistics.
        /// </value>
        //public virtual IDbSet<CryStatistic> CryStatistics { get; set; }


        #endregion

        protected override void OnModelCreating(DbModelBuilder modelBuilder) {
            base.OnModelCreating(modelBuilder);
            modelBuilder.Entity<Role>().ToTable("webpages_Roles");
            modelBuilder.Entity<Role>().Property(p => p.Name).HasColumnName("RoleName");
            modelBuilder.Entity<Role>().Property(p => p.Id).HasColumnName("RoleId");
            modelBuilder.Entity<Role>().HasKey(p => p.Id);
            modelBuilder.Entity<Role>().HasMany<User>(r => r.Users).WithMany(u => u.Roles).Map(x => {
                x.ToTable("webpages_UsersInRoles");
                x.MapRightKey("UserId");
                x.MapLeftKey("RoleId");
            });

            modelBuilder.Entity<Membership>().ToTable("webpages_Membership");
            modelBuilder.Entity<Membership>().HasKey(m => m.UserId);
            modelBuilder.Entity<Membership>().Property(m => m.UserId).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);

            modelBuilder.Entity<OAuthMembership>().ToTable("webpages_OAuthMembership");
            modelBuilder.Entity<OAuthMembership>().HasKey(o => new { o.Provider, o.ProviderUserId });
            
        }

    }
}